<!--
  Crash Space Sign App
  2010 Tod E. Kurt, http://todbot.com/
  version: 20101104 
-->
<html>
<head>
<link rel="alternate" type="application/rss+xml" title="CrashSpace Sign RSS Feed" href="http://crashspacela.com/sign/feed/" />	
</head>
<body bgcolor=#eeeeee fgcolor=#000000>

<center>
<br />
<br />
<br />
<br />
<br />
<?php 
date_default_timezone_set('America/Los_Angeles');

//
function read_file($file, $lines)
{
    $handle = fopen($file, "r");
    fseek($handle, 0);
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; break;
            }
            $t = fgetc($handle);
            $pos--;
        }
        $linecounter--;
        if($beginning) rewind($handle);
        $text[$lines-$linecounter-1] = fgets($handle);
        if($beginning) break;
    }
    fclose ($handle);
    // array_reverse is optional: 
    // can just return the $text array which consists of the file's lines.
    //return array_reverse($text); 
    return $text; 
}

// ---------------------------------------------------------------------------

$diff_mins_max_default = 60 * 1;  // 60 mins * 1 hour
$lastcount = 5;  // how many lines of updates to show

$file = "updates.txt"; 

$id            = $_GET['id'];
$type          = $_GET['type'];
$diff_mins_max = $_GET['diff_mins_max'];
$msg           = $_GET['msg'];
$debug         = $_GET['debug'];

$ip_addr = $_SERVER['REMOTE_ADDR'];
#$ip_addr_allowed = "76.79.191.5";
#$ip_addr_allowed = "24.43.180.42";
$ip_addr_allowed = gethostbyname('collision.crashspace.org');

if( !$diff_mins_max || $diff_mins_max == 0 ) {
    $diff_mins_max = $diff_mins_max_default;
}

if( !$type || $type=="" ) {
    $type = "update";
}

$diff_secs_max = $diff_mins_max * 60;
$diff_secs = 0;

$dfmt = "Y-m-d, g:i a";

$now_time = time();

if( !$debug && $id && $ip_addr != $ip_addr_allowed ) {
    print "<h2> Sorry, you are not authorized to update, you must be in the space </h2>\n";
    $id = 0;
}

// read in the last 'lastcount' lines of the update log
$lines = read_file($file, $lastcount);
list( $count, $update_type, $old_id, $old_time, $old_diff_secs_max, $old_msg)= 
    explode(":",rtrim($lines[0]));

// is there an update?
if( $id ) {
    
    $count++;  // increment update counter
    
    //print "<h3> You ('". $id ."') are update number ". $count .
    //  " at ". date($dfmt,$now_time) . "</h3>\n";
    
    $data = "$count:$type:$id:$now_time:$diff_secs_max:$msg\n";
    
    $handle = fopen($file, 'a+'); // append
    fwrite($handle, $data);  // save the update to the file
    fclose($handle);         // closes the file 

    $old_time = $now_time;
}
else {
    $msg = $old_msg;
    $id = $old_id;
    $diff_secs_max = $old_diff_secs_max;
}

$diff_mins_max = $diff_secs_max / 60;
    
$diff_secs = $now_time - $old_time;


//printf( "<h3> Last id was '%s', %d minutes ago, at %s </h3>\n",
//            $id, $diff_mins, date($dfmt,$old_time) );

// how long until closing
$diff_mins   = $diff_secs/60;
$delta_mins  = $diff_mins_max - $diff_mins;
$future_time = $now_time + ($delta_mins*60);

print "\n<!-- diff_secs: ". $diff_secs .", delta_mins:". $delta_mins ." -->\n";

// default to assuming it's closed :-)
$signmsg = "Crash Space is CLOSED";
$color = "#FF3333";
if( $diff_secs >= 0 && $diff_secs < $diff_secs_max ) {
    $signmsg = "Crash Space is OPEN";
    $color = "#33FF33";
}

print "\n<table border=0 cellpadding=20 bgcolor=\"$color\"><tr>";
print '<td><img src="crashlogo-small.png"></td>';
print "<td><font size=+10><b>$signmsg</b></font></td>";
print '<td><img src="crashlogo-small.png"></td>';
print "</tr></table>\n";

if( $delta_mins >= 0 ) {
    printf( "<h3>The space will close in %2.2d minutes, at %s</h3>\n", 
            $delta_mins, date($dfmt,$future_time) );
}

print "<br /><br /><br />\n";

if( $msg != '' && $msg != 'none' ) {
    print "<h2>message from $id: $msg </h2>\n";
}


$lines = read_file($file, $lastcount);
list( $count, $update_type, $old_id, $old_time, $old_diff_secs_max, $old_msg)= 
    explode(":",rtrim($lines[0]));

print "<b> Last $lastcount updates </b><br />\n";
print "<table border=1 cellpadding=5>\n";
print "<tr><th> id </th><th>type</th><th> time </th><th> mins </th><th> message </th></tr>\n";
foreach ($lines as $line) {
    list($count,$type,$id,$time,$diff_secs_max,$msg) = explode(":",rtrim($line));
    $tt = date( $dfmt, $time);
    $mins = $diff_secs_max / 60;
    print "<tr><td> $id </td><td>$type</td><td> $tt </td>".
        "<td> $mins </td><td> $msg </td></tr>\n";
}
print "</table>\n";

?>


<a href="http://crashspace.org/">CrashSpace.org</a>
</center>

<script type="text/javascript">

    var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18081664-2']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>
</body>
</html>
